home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C24 / Constcst.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  633 b   |  27 lines

  1. //: C24:Constcst.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Const casts
  7.  
  8. int main() {
  9.   const int i = 0;
  10.   int* j = (int*)&i; // Deprecated form
  11.   j  = const_cast<int*>(&i); // Preferred
  12.   // Can't do simultaneous additional casting:
  13. //! long* l = const_cast<long*>(&i); // Error
  14.   volatile int k = 0;
  15.   int* u = const_cast<int*>(&k);
  16. }
  17.  
  18. class X {
  19.   int i;
  20. // mutable int i; // A better approach
  21. public:
  22.   void f() const {
  23.     // Casting away const-ness:
  24.     (const_cast<X*>(this))->i = 1;
  25.   }
  26. }; ///:~
  27.